<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace hdphp\utils;

use think\App;

/**
 * Class：Start输出类
 * 创建人：Jack
 * 创建时间：2023/12/29 17:15
 * @package hdphp\utils
 */
class Start
{
    public function show()
    {
        $this->opCacheClear();

        $context = $this->logo();
        $context .= $this->displayItem('php      version', phpversion());
        $context .= $this->displayItem('swoole   version', phpversion('swoole'));
        $context .= $this->displayItem('thinkphp version', App::VERSION);
        $context .= $this->displayItem('HdAdmin    version', get_version());

        // http配置
        $httpConf = \config("swoole.http");
        $context .= $this->displayItem('http enable', $httpConf["enable"]);
        $context .= $this->displayItem('http host', $httpConf["host"]);
        $context .= $this->displayItem('http port', $httpConf["port"]);
        $context .= $this->displayItem('http worker_num', $httpConf["worker_num"]);

        // websocket配置
        $context .= $this->displayItem('websocket enable', \config("swoole.websocket.enable"));

        // rpc配置
        $rpcConf = \config("swoole.rpc.server");
        $context .= $this->displayItem('rpc enable', $rpcConf["enable"]);
        if ($rpcConf["enable"]) {
            $context .= $this->displayItem('rpc host', $rpcConf["host"]);
            $context .= $this->displayItem('rpc port', $rpcConf["port"]);
            $context .= $this->displayItem('rpc worker_num', $rpcConf["worker_num"]);
        }

        // 队列配置
        $context .= $this->displayItem('queue enable', \config("swoole.queue.enable"));

        // 热更新配置
        $context .= $this->displayItem('hot_update enable', (bool)\config("swoole.hot_update.enable"));

        // debug配置
        $context .= $this->displayItem('app_debug enable', (bool)env("APP_DEBUG"));

        // 打印信息
        echo $context;
    }

    /**
     * 注释：logo
     * 创建人：Jack
     * 创建时间：2024/1/10 10:41
     * @return string
     */
    private function logo()
    {
        return <<<LOGO
             ____  ____  ______   _______  ____  ____  _______   
            |_   ||   _||_   _ `.|_   __ \|_   ||   _||_   __ \  
              | |__| |    | | `. \ | |__) | | |__| |    | |__) | 
              |  __  |    | |  | | |  ___/  |  __  |    |  ___/  
             _| |  | |_  _| |_.' /_| |_    _| |  | |_  _| |_     
            |____||____||______.'|_____|  |____||____||_____|
  

LOGO;
    }

    private function displayItem($name, $value)
    {
        if ($value === true) {
            $value = 'true';
        } else if ($value === false) {
            $value = 'false';
        } else if ($value === null) {
            $value = 'null';
        }

        return "\e[32m" . str_pad($name, 30, ' ', STR_PAD_RIGHT) . "\e[34m" . $value . "\e[0m \n";
    }

    private function opCacheClear()
    {
        if (function_exists('apc_clear_cache')) {
            apc_clear_cache();
        }
        if (function_exists('opcache_reset')) {
            opcache_reset();
        }
    }
}
